import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 准备数据
data = {
    '日期': ['2021Q1', '2021Q2', '2021Q3', '2021Q4', '2022Q1', '2022Q2'],
    '销售额': [3121, 4086, 4321, 4601, 4936, 4231],
    '利润额': [1020, 1421, 1502, 1623, 1781, 1432]
}

df = pd.DataFrame(data)

# 创建柱状图
fig, ax = plt.subplots(figsize=(12, 7))
bar_width = 0.35

# 设置背景颜色
fig.patch.set_facecolor('#001A4D')
ax.set_facecolor('#001A4D')

# 设置柱状图的位置
r1 = np.arange(len(df['销售额']))
r2 = [x + bar_width for x in r1]

# 绘制销售额和利润额的柱状图
ax.bar(r1, df['销售额'], color='#007bff', width=bar_width, edgecolor='none', label='销售额')
ax.bar(r2, df['利润额'], color='#ff4057', width=bar_width, edgecolor='none', label='利润额')

# 添加数据标签
for i in range(len(r1)):
    ax.text(r1[i], df['销售额'][i] + 100, df['销售额'][i], ha='center', va='bottom', color='white', fontsize=10)
    ax.text(r2[i], df['利润额'][i] + 100, df['利润额'][i], ha='center', va='bottom', color='white', fontsize=10)

# 添加标题和标签
plt.xlabel('')
plt.ylabel('')
plt.title('2021年至今季度销售额(万)和利润额(万)', color='white', fontsize=20)
plt.text(-0.5, max(df['销售额']) + 800, '2022年第二季度销售额首次出现下降，降幅达到15%', ha='left', va='center',
         color='white', fontsize=18)

# 设置x轴和y轴的刻度标签
plt.xticks([r + bar_width / 2 for r in range(len(df['销售额']))], df['日期'], color='white', fontsize=10)
plt.yticks(color='white', fontsize=10)

# 隐藏x轴和y轴的标题
ax.set_xlabel('')
ax.set_ylabel('')

# 添加图例
legend = plt.legend(loc='upper right', frameon=False)
for text in legend.get_texts():
    text.set_color('white')

# 去除图框
plt.box(False)

# 显示图表
plt.tight_layout()
plt.show()
